package com.zzyl.nursing.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zzyl.common.utils.spring.SpringUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约过期处理任务
 * 超过预约时间一小时未到访，预约单状态变为已过期
 */
@Component("reservationExpiredJob")
public class ReservationExpiredJob implements Job {
    
    private static final Logger log = LoggerFactory.getLogger(ReservationExpiredJob.class);

    /**
     * 若依框架要求的定时任务执行方法
     * 
     * @param jobExecutionContext 任务执行上下文
     * @throws Exception 异常
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        execute();
    }
    
    /**
     * 实际的业务执行方法，供若依框架通过反射调用
     * 方法名需要与定时任务配置中的方法名一致
     */
    public void execute() {
        log.info("开始执行预约过期处理任务");
        
        try {
            // 通过SpringUtils获取Mapper实例
            ReservationMapper reservationMapper = SpringUtils.getBean(ReservationMapper.class);
            
            // 使用自定义的updateExpiredReservations方法批量更新
            int updatedCount = reservationMapper.updateExpiredReservations();
            log.info("更新了 {} 条已过期的预约记录", updatedCount);
            
            log.info("预约过期处理任务执行完成");
        } catch (Exception e) {
            log.error("执行预约过期处理任务时发生异常", e);
            throw new RuntimeException("预约过期处理任务执行失败: " + e.getMessage(), e);
        }
    }
}
